<template>
  <div class="mod-demo-echarts">
    <el-alert
      title="此处应该是一个溯源树，可以从最基础的物料开始，查到物料的最终用途"
      type="warning"
      :closable="false">

    </el-alert>

    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input  placeholder="材料ID" clearable></el-input>
<!--        v-model="dataForm.key"-->
      </el-form-item>

      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('generator:tbasecraft:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('generator:tbasecraft:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>


    <el-row :gutter="20">

      <!--展示图表-->
      <!--展示图表-->
      <el-col :span="24">
        <el-card>
          <div id="J_chartPieBox" class="chart-box" style="height: 100%"></div>
        </el-card>
      </el-col>

    </el-row>
  </div>
</template>

<script>
import * as echarts from 'echarts/core'
// 引入柱状图图表，图表后缀都为 Chart
// eslint-disable-next-line import/no-duplicates
import { BarChart } from 'echarts/charts'
// eslint-disable-next-line import/no-duplicates
import {TreeChart} from 'echarts/charts'
// 引入提示框，标题，直角坐标系，数据集，内置数据转换器组件，组件后缀都为 Component
import {
  LegendComponent,
  TitleComponent,
  TooltipComponent,
  GridComponent,
  DatasetComponent,
  ToolboxComponent,
  TransformComponent
} from 'echarts/components'
// 标签自动布局，全局过渡动画等特性
import { LabelLayout, UniversalTransition } from 'echarts/features'
// 引入 Canvas 渲染器，注意引入 CanvasRenderer 或者 SVGRenderer 是必须的一步
import { CanvasRenderer } from 'echarts/renderers'
// eslint-disable-next-line import/no-duplicates
import { LineChart } from 'echarts/charts'
// 注册必须的组件
echarts.use([
  ToolboxComponent,
  TreeChart,
  TitleComponent,
  LegendComponent,
  TooltipComponent,
  GridComponent,
  DatasetComponent,
  TransformComponent,
  BarChart,
  LabelLayout,
  UniversalTransition,
  LineChart,
  CanvasRenderer
])

export default {
  data () {
    return {
      chartTree: null,
      dataList: []
    }
  },
  mounted () {
    // this.initChartLine()
    // this.initChartBar()
    this.initChartTree()
    // this.initChartScatter()
  },
  activated () {
    // 由于给echart添加了resize事件, 在组件激活时需要重新resize绘画一次, 否则出现空白bug
    if (this.chartTree) {
      this.chartTree.resize()
    }
  },
  methods: {
    getDataList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/generator/devicelist/sys/tree'),
        method: 'get'
      }).then(({data}) => {
        if (data && data) {
          this.dataList = data
          this.totalPage = data.page
          console.log(this.totalPage)
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 树图
    initChartTree: function () {
      var data = {
        'name': '原始材料',
        'children': [
          {
            'name': '初始组件1',
            'children': [
              {
                'name': '组件1',
                'children': [
                  {
                    'name': '产品1',
                    'value': 3938
                  },
                  {
                    'name': '产品2',
                    'value': 3812
                  },
                  {
                    'name': '产品3',
                    'value': 6714
                  },
                  {
                    'name': '产品4',
                    'value': 743
                  }
                ]
              },
              {
                'name': 'graph',
                'children': [
                  {
                    'name': 'BetweennessCentrality',
                    'value': 3534
                  },
                  {
                    'name': 'LinkDistance',
                    'value': 5731
                  },
                  {
                    'name': 'MaxFlowMinCut',
                    'value': 7840
                  },
                  {
                    'name': 'ShortestPaths',
                    'value': 5914
                  },
                  {
                    'name': 'SpanningTree',
                    'value': 3416
                  }
                ]
              },
              {
                'name': 'optimization',
                'children': [
                  {
                    'name': 'AspectRatioBanker',
                    'value': 7074
                  }
                ]
              }
            ]
          },
          {
            'name': '初始组件2',
            'children': [
              {
                'name': '组件1',
                'value': 17010
              },
              {
                'name': '组件2',
                'value': 5842
              },
              {
                'name': '组件3',
                'children': [
                  {
                    'name': '产品1',
                    'value': 1983
                  },
                  {
                    'name': '产品2',
                    'value': 2047
                  },
                  {
                    'name': '产品3',
                    'value': 1375
                  },
                  {
                    'name': '产品4',
                    'value': 8746
                  },
                  {
                    'name': '产品5',
                    'value': 2202
                  },
                  {
                    'name': '产品6',
                    'value': 1382
                  },
                  {
                    'name': '产品7',
                    'value': 1629
                  },
                  {
                    'name': '产品8',
                    'value': 1675
                  },
                  {
                    'name': '产品9',
                    'value': 2042
                  }
                ]
              },
              {
                'name': '产品10',
                'value': 1041
              },
              {
                'name': '产品11',
                'value': 5176
              }
            ]
          },
          {
            'name': '初始组件3',
            'children': [
              {
                'name': '组件1',
                'children': [
                  {
                    'name': '产品1',
                    'value': 721
                  },
                  {
                    'name': '产品2',
                    'value': 4294
                  },
                  {
                    'name': '产品3',
                    'value': 9800
                  },
                  {
                    'name': '产品4',
                    'value': 1314
                  },
                  {
                    'name': '产品5',
                    'value': 2220
                  }
                ]
              }
            ]
          },
          {
            'name': '初始组件4',
            'children': [
              {
                'name': 'DirtySprite',
                'value': 8833
              },
              {
                'name': 'LineSprite',
                'value': 1732
              },
              {
                'name': 'RectSprite',
                'value': 3623
              },
              {
                'name': 'TextSprite',
                'value': 10066
              }
            ]
          },
          {
            'name': '初始组件5',
            'children': [
              {
                'name': 'FlareVis',
                'value': 4116
              }
            ]
          }
        ]
      }
      var option = {
        tooltip: {
          trigger: 'item',
          triggerOn: 'mousemove'
        },
        series: [
          {
            type: 'tree',
            data: [data],
            top: '1%',
            left: '7%',
            bottom: '1%',
            right: '20%',
            symbolSize: 9,
            label: {
              position: 'left',
              verticalAlign: 'middle',
              align: 'right',
              fontSize: 14
            },
            leaves: {
              label: {
                position: 'right',
                verticalAlign: 'middle',
                align: 'left'
              }
            },
            emphasis: {
              focus: 'descendant'
            },
            expandAndCollapse: true,
            animationDuration: 550,
            animationDurationUpdate: 750
          }
        ]
      }
      this.chartTree = echarts.init(document.getElementById('J_chartPieBox'))
      this.chartTree.setOption(option)
      window.addEventListener('resize', () => {
        this.chartTree.resize()
      })
    },
    created () {
      this.getDataList()
    }
  }
}
</script>

<style lang="scss">
.mod-demo-echarts {
  > .el-alert {
    margin-bottom: 10px;
  }
  > .el-row {
    margin-top: -20px;
    margin-bottom: -20px;
    .el-col {
      padding-top: 20px;
      padding-bottom: 10px;
    }
  }
  .chart-box {
    min-height: 1000px;
  }
}
</style>


<!--
    // 柱状图
    initChartBar () {
      var option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {
          data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎', '百度', '谷歌', '必应', '其他']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: '直接访问',
            type: 'bar',
            data: [320, 332, 301, 334, 390, 330, 320]
          },
          {
            name: '邮件营销',
            type: 'bar',
            stack: '广告',
            data: [120, 132, 101, 134, 90, 230, 210]
          },
          {
            name: '联盟广告',
            type: 'bar',
            stack: '广告',
            data: [220, 182, 191, 234, 290, 330, 310]
          },
          {
            name: '视频广告',
            type: 'bar',
            stack: '广告',
            data: [150, 232, 201, 154, 190, 330, 410]
          },
          {
            name: '搜索引擎',
            type: 'bar',
            data: [862, 1018, 964, 1026, 1679, 1600, 1570],
            markLine: {
              lineStyle: {
                normal: {
                  type: 'dashed'
                }
              },
              data: [
                [{ type: 'min' }, { type: 'max' }]
              ]
            }
          },
          {
            name: '百度',
            type: 'bar',
            barWidth: 5,
            stack: '搜索引擎',
            data: [620, 732, 701, 734, 1090, 1130, 1120]
          },
          {
            name: '谷歌',
            type: 'bar',
            stack: '搜索引擎',
            data: [120, 132, 101, 134, 290, 230, 220]
          },
          {
            name: '必应',
            type: 'bar',
            stack: '搜索引擎',
            data: [60, 72, 71, 74, 190, 130, 110]
          },
          {
            name: '其他',
            type: 'bar',
            stack: '搜索引擎',
            data: [62, 82, 91, 84, 109, 110, 120]
          }
        ]
      }
      this.chartBar = echarts.init(document.getElementById('J_chartBarBox'))
      this.chartBar.setOption(option)
      window.addEventListener('resize', () => {
        this.chartBar.resize()
      })
    },
-->
